<?php

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Employee;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\Cashier\Collection;
use AuctionCore\Common\AjaxPage;
use AuctionCore\Account\Back\Department;

/**
 * 收银台收款
 */
class CollectionController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $collectionTypeList = C("COLLECTION_TYPE");
        $collectionWay = C("COLLECTION_WAY");
        $this->assign("collection_type_list", $collectionTypeList);
        $this->assign("collection_way", $collectionWay);
    }

    public function index()
    {
        $branchId = $this->user_info['branch_id'];
        if($branchId == 0){
            $branchList = Department::getBranchCompanyList();
            $this->assign("branchList",$branchList);
        }else{
            $branchName = Department::getBranchCompanyByDepartId($branchId);
            $this->assign("branchName",$branchName);
        }
        $this->assign('branchId',$branchId);
        $type_list = C("COLLECTION_TYPE");
        $this->assign("type_list", $type_list);
        $this->display();
    }

    //收款列表
    public function collectionList()
    {
        $page = I("get.p/d", 1);
        $limit = 10;
        $t = I('get.t', 0);
        //$order_no = I("get.order_no", '', 'trim');
        $car_no = I("get.car_no", '', 'trim');
        $purchase_order_no = I("get.purchase_order_no", '', 'trim');
        $change_order_no = I("get.change_order_no", '', 'trim');
        $collection_no = I("get.collection_no", '', 'trim');
        $branch_id = $this->user_info['branch_id'];
        if($branch_id == 0){
            $branch_id = I("get.branch_id/d", -1);
        }
        $payment_type = I('get.payment_type/d', 0);
        $start_time = I('get.start_time', '');
        $end_time = I('get.end_time', '');
        $params = [
            'status' => $t,
            'car_no' => $car_no,
            'branch_id' => $branch_id,
            //'order_no' => $order_no,
            'collection_no' => $collection_no,
            'change_order_no' => $change_order_no,
            'purchase_order_no' => $purchase_order_no,
            'payment_type' => $payment_type,
            'start_time' => $start_time,
            'end_time' => $end_time,
        ];
        $result = Collection::getList($page, $limit, $params);
        $total = $result['total'] > 0 ? $result['total'] : 0;
        $list = empty($result['list']) ? array() : $result['list'];
        $Page = new AjaxPage($total, $limit, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $page_show = $Page->show();
        $this->assign('p', $page);
        $this->assign('t', $t);
        $this->assign('page', $page_show);
        $this->assign('params', $params);
        $this->assign('list', $list);
        $this->display('Collection/list');
    }

    public function view()
    {
        $cid = I('get.cid/d', 0);
        if ($cid < 1) {
            $this->error('参数错误');
        }
        $co = new Collection($cid);
        $info = $co->getInfo();
        if ($info['status'] != 1) {
            $info['update_time'] = '--';
            $info['emp_name'] = '--';
        }
        $statusGroup = Collection::$statusGroup;
        if (in_array($info['payment_type'], $statusGroup['ORDER'])) {
            $data = Collection::getOrderInfoById($cid);
            $info['order_no'] = $data['order_no'];
            $info['car_source_nu'] = $data['sno'];
            $info['purchase_order_nu'] = $data['purchase_order_nu'];
            $info['change_no'] = "--";
            //获取价格信息
            $fee = AuOrder::getOrderFee($data['order_id']);
            $this->assign('fee', $fee);
        } elseif (in_array($info['payment_type'], $statusGroup['CHANGE'])) {
            if ($info['refer'] > 0) {
                $isDefault = $info['payment_type'] == 8 ? 0 : 1;
                $data = Collection::getRefundCarInfoByCollectionId($info['id'], $isDefault);
                $info['change_no'] = "--";
            }else{
                $data = Collection::getChangeInfoById($cid);
                $info['change_no'] = $data['modify_no'];
            }
            $info['order_no'] = $data['order_no'];
            $info['car_source_nu'] = $data['sno'];
            $info['purchase_order_nu'] = $data['purchase_order_nu'];

        } elseif ($info['payment_type'] == 5 || $info['payment_type'] == 9) {
            $info['order_no'] = '--';
            $info['purchase_order_nu'] = '--';
            $info['car_source_nu'] = '--';
            $info['change_no'] = '--';
        }
        $e = new Employee($this->user_info['emp_id']);
        $lockAuth = $e->check_auth("ShouKuanDanSuoDing");
        $unlockAuth = $e->check_auth("ShouKuanDanJieSuo");
        $this->assign("lockAuth",$lockAuth);
        $this->assign("unlockAuth",$unlockAuth);
        $this->assign("status_group", $statusGroup);
        $info['status_cn'] = C('COLLECTION_STATUS.' . $info['status']);
        $this->assign('info', $info);
        $this->display();
    }

    //确认收款
    public function confirm()
    {
        $cid = I('post.cid/d', 0);
        if ($cid < 1) {
            $this->helpJsonResult(400, '参数错误');
        }
        $co = new Collection($cid);
        $info = $co->getInfo();
        if ($info['status'] != 0) {
            $this->helpJsonResult(400, '状态错误');
        }

        $data = [];
        $data['poundage'] = I("post.poundage/f");
        if (!empty($data['poundage']) && !preg_match("/^[0-9]+(.[0-9]{0,2})?$/",$data['poundage'])) {
            $this->helpJsonResult(400, '所填写金额不得超过小数点后两位');
        }
        $data['emp_id'] = $this->user_info['emp_id'];
        $data['emp_name'] = $this->user_info['real_name'];
        $data['status'] = 1;
        $data['update_time'] = date("Y-m-d H:i:s");
        $data['payment_type'] = $info['payment_type'];
        $data['amount'] = $info['amount'];
        $data['refer'] = $info['refer'];


        $co = new Collection($cid);
        $res = $co->confirmCollection($data);
        if ($res === false) {
            $this->helpJsonResult(500, "确认失败");
        }
        $this->helpJsonResult(200, "确认成功");
    }

    //收款单锁定，解锁
    public function lock()
    {
        $cid = I("post.cid/d",0);
        if ($cid < 1) {
            $this->helpJsonResult(400, '参数错误');
        }
        $co = new Collection($cid);
        $info = $co->getInfo();
        if ($info['status'] != 0) {
            $this->helpJsonResult(400, '收款单状态不正确');
        }
        $data = [];
        $data['emp_id'] = $this->user_info['emp_id'];
        $data['emp_name'] = $this->user_info['real_name'];
        $act = I("post.act/d",0);
        $res = true;;
        if($act ==1){ //锁定
            $res = Collection::lock($cid,$data['emp_id'],$data['emp_name']);
        }elseif ($act==2){//解锁
            $res = Collection::unlock($cid,$data['emp_id'],$data['emp_name']);
        }
        if(!$res){
            $this->helpJsonResult(500,"操作失败");
        }
        $this->helpJsonResult(200,"操作成功");
    }
}
